package com.yz.jdd.module.bind.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.yz.jdd.base.MyApplication;
import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.module.bind.view.BindAccountActivity;
import com.yz.jdd.module.bind.view.BindView;
import com.yz.jdd.network.MySubscriber;
import com.yz.jdd.network.NetWorkRequset;
import com.yz.jdd.network.request.Request_BindAccount;
import com.yz.jdd.network.request.Request_UserLogin;
import com.yz.jdd.network.request.Request_VerifyCode;
import com.yz.jdd.network.request.Requset_usercenter;
import com.yz.jdd.network.response.Response_UserLogin;
import com.yz.jdd.network.response.Response_accountId;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by cc on 2018/2/8.
 */

public class BindPresenter {
    private BindView bindView;
    Request_UserLogin request_bindAccount;

    Requset_usercenter request_verifyCode;

    public void setAliUserId(String aliUserId) {
        this.aliUserId = aliUserId;
    }

    private String aliUserId;

    private String accountId;

    public BindPresenter(BindView bindView) {
        this.bindView = bindView;

    }

    /**
     * 获取验证码
     */
    public void doGetVerifyCode() {

        String phoneNum = bindView.getPhoneNum();
        if (TextUtils.isEmpty(phoneNum)) {
            bindView.showToastDialog("请输入手机号码");
        } else {

//            String phone = "^1[34578]\\d{9}$";
            String phone = "^1\\d{10}$";
            Pattern pattern = Pattern.compile(phone);
            Matcher matcher = pattern.matcher(phoneNum);

            bindView.disableVerifyCode();
            if (matcher.matches()) {
                request_verifyCode = new Requset_usercenter();
                request_verifyCode.setPhoneNum(phoneNum);
                request_verifyCode.setAliUserId(aliUserId);
                NetWorkRequset.doGetBindVerifyCode(request_verifyCode, new MySubscriber() {
                    @Override
                    public void onSuccess(Response_Base response_baseModel) {
                        bindView.enableVerifyCode();
                        bindView.startTimer();
                        Response_accountId response_accountId = (Response_accountId) response_baseModel.getResultObject();
                        if (response_accountId != null)
                            accountId = response_accountId.getAccountId();
                        if (!TextUtils.isEmpty(response_baseModel.getMsg()))
                            bindView.showToastDialog(response_baseModel.getMsg());
                    }

                    @Override
                    public void onNeedLogin(String msg) {
//                        bindView.enableVerifyCode();
//                        bindView.startLoginActivity();
                    }

                    @Override
                    public void onFail(Response_Base response_baseModel) {
                        bindView.enableVerifyCode();
                        bindView.showToastDialog(response_baseModel.getMsg());
                    }

                });

            } else {
                bindView.enableVerifyCode();
                bindView.showToastDialog("请输入有效的手机号");
            }
        }
    }

    /**
     * 绑定手机号
     */
    public void doBindAccount() {

        String verifyCode = bindView.getVerifyCode();
        String phoneNum = bindView.getPhoneNum();


        if (TextUtils.isEmpty(phoneNum)) {
            bindView.showToastDialog("请输入手机号码");
        } else {

//            String phone = "^1[34578]\\d{9}$";
            String phone = "^1\\d{10}$";
            Pattern pattern = Pattern.compile(phone);
            Matcher matcher = pattern.matcher(phoneNum);
            if (matcher.matches()) {
                if (TextUtils.isEmpty(verifyCode)) {
                    bindView.showToastDialog("请输入验证码");
                } else {
                    bindView.showLoading("正在绑定");
                    request_bindAccount = new Request_UserLogin();

                    request_bindAccount.setAliUserId(aliUserId);
                    request_bindAccount.setVeryCode(verifyCode);
                    request_bindAccount.setPhoneNum(phoneNum);
//                    request_bindAccount.setAccount("");
                    request_bindAccount.setPassword("");
                    request_bindAccount.setAccount(TextUtils.isEmpty(accountId) ? "" : accountId);
                    NetWorkRequset.doBindLogin(request_bindAccount, new MySubscriber() {
                        @Override
                        public void onSuccess(Response_Base response_baseModel) {
                            bindView.dismissLoading();
                            if (!TextUtils.isEmpty(response_baseModel.getMsg()))
                                bindView.showToastDialog("绑定成功");
                            Response_UserLogin response = (Response_UserLogin) response_baseModel.getResultObject();
                            if (response != null) {
                                PreferencesUtils.putString(
                                        MyApplication.application,
                                        BaseConfigs.PREF_USERNAME, response.getUserName());
                                PreferencesUtils.putString(
                                        MyApplication.application,
                                        BaseConfigs.PREF_SHOPNAME, response.getShopName());

                                PreferencesUtils.putString(MyApplication.application, BaseConfigs
                                        .STRING_ROUTERCODE, response.getRouterCode());

                                PreferencesUtils.putString(MyApplication.application, BaseConfigs
                                        .POS_SUPPLIERCODE, response.getSupplierCode());

                                PreferencesUtils.putBoolean(MyApplication.application, BaseConfigs
                                        .PREF_VERIFICATION_FLAG, response.isVerificationFlag());

                                String sessionId = response.getSessionId();
                                if (!TextUtils.isEmpty(sessionId)) {

                                    PreferencesUtils.putString(
                                            MyApplication.application,
                                            BaseConfigs.PREF_SESSIONID, sessionId);
                                }
                                String regionName=response.getRegionName();
                                PreferencesUtils.putString(MyApplication.application,
                                        BaseConfigs.STRING_REGIONNAME, regionName);

                                if (response.getMenuInfo() != null) {
                                    PreferencesUtils.putString(
                                            MyApplication.application,
                                            BaseConfigs.PREF_MENUINFO, new Gson().toJson(response.getMenuInfo()));
                                }
                                String rc=response.getRc();
                                if (!TextUtils.isEmpty(rc)) {

                                    PreferencesUtils.putString(
                                            MyApplication.application,
                                            BaseConfigs.STRING_ROUTE_CODE, rc);
                                }


                                bindView.setH5WebActivity();
                                bindView.close();
                            }

                        }

                        @Override
                        public void onNeedLogin(String msg) {

                        }

                        @Override
                        public void onFail(Response_Base response_baseModel) {
                            bindView.dismissLoading();
                            if (!TextUtils.isEmpty(response_baseModel.getMsg()))
                                bindView.showToastDialog(response_baseModel.getMsg());
                        }

                    });
                }
            } else {
                bindView.showToastDialog("请输入有效的手机号");
            }
        }
    }
}
